Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.10% |
27 / 29 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
| InseuisAnalyser | |
93.10% |
27 / 29 |
|
71.43% |
5 / 7 |
11.04 | |
0.00% |
0 / 1 |
| analyse | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| checkTransaction | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
1.00 | |||
| checkSpan | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
1.01 | |||
| checkHttpSpan | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isRequestTransactionSuccessful | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| isJobTransactionSuccessful | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isCommandTransactionSuccessful | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Inseuis\Services; |
| 4 | |
| 5 | use Illuminate\Support\Collection; |
| 6 | use Inseuis\Contracts\InseuisAnalyserContract; |
| 7 | use Inseuis\Struct\Spans\AbstractSpan; |
| 8 | use Inseuis\Struct\Spans\HttpSpan; |
| 9 | use Inseuis\Struct\Spans\QuerySpan; |
| 10 | use Inseuis\Struct\Spans\RedisCommandSpan; |
| 11 | use Inseuis\Struct\Transactions\AbstractTransaction; |
| 12 | use Inseuis\Struct\Transactions\CommandTransaction; |
| 13 | use Inseuis\Struct\Transactions\JobTransaction; |
| 14 | use Inseuis\Struct\Transactions\RequestTransaction; |
| 15 | |
| 16 | class InseuisAnalyser implements InseuisAnalyserContract |
| 17 | { |
| 18 | /** |
| 19 | * @param Collection<array-key, AbstractSpan> $spans |
| 20 | */ |
| 21 | public function analyse(AbstractTransaction $transaction, Collection $spans, ?int $allowedExitCode): void |
| 22 | { |
| 23 | $this->checkTransaction($transaction, $allowedExitCode); |
| 24 | foreach ($spans as $span) { |
| 25 | $this->checkSpan($span); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | protected function checkTransaction(AbstractTransaction $transaction, ?int $allowedExitCode): void |
| 30 | { |
| 31 | $transaction->successful = match (true) { |
| 32 | $transaction instanceof RequestTransaction => $this->isRequestTransactionSuccessful( |
| 33 | $transaction, |
| 34 | $allowedExitCode |
| 35 | ), |
| 36 | $transaction instanceof JobTransaction => $this->isJobTransactionSuccessful($transaction), |
| 37 | $transaction instanceof CommandTransaction => $this->isCommandTransactionSuccessful( |
| 38 | $transaction, |
| 39 | $allowedExitCode |
| 40 | ), |
| 41 | default => null |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | protected function checkSpan(AbstractSpan $span): void |
| 46 | { |
| 47 | $span->successful = match (true) { |
| 48 | $span instanceof HttpSpan => $this->checkHttpSpan($span), |
| 49 | $span instanceof QuerySpan, $span instanceof RedisCommandSpan => true, |
| 50 | default => null |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | protected function checkHttpSpan(HttpSpan $span): bool |
| 55 | { |
| 56 | return $span->responseCode < 400; |
| 57 | } |
| 58 | |
| 59 | protected function isRequestTransactionSuccessful(RequestTransaction $transaction, ?int $allowedExitCode): bool |
| 60 | { |
| 61 | foreach ($transaction->getErrors() as $error) { |
| 62 | if (!$error->handled) { |
| 63 | return $transaction->responseCode < 500; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if ($allowedExitCode !== null) { |
| 68 | return $transaction->responseCode === $allowedExitCode; |
| 69 | } |
| 70 | |
| 71 | return $transaction->responseCode < 500; |
| 72 | } |
| 73 | |
| 74 | protected function isJobTransactionSuccessful(JobTransaction $transaction): bool |
| 75 | { |
| 76 | return !$transaction->failed; |
| 77 | } |
| 78 | |
| 79 | protected function isCommandTransactionSuccessful(CommandTransaction $transaction, ?int $allowedExitCode): bool |
| 80 | { |
| 81 | return $transaction->exitCode === ($allowedExitCode ?? 0); |
| 82 | } |
| 83 | } |